﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sharp.Infrastructure;
using Sharp.Platform;
using Sharp.Platform.Configuration;

namespace Sharp.Web.Views.Setting
{
    [ModuleId("SystemConfig"), Sharp.Platform.Title("系统配置")]
    public partial class SystemConfig : ViewPage
    {
        public SystemConfig()
        {
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["Action"] != null)
            {
                var res = Save(Request["typeFullName"]);
                Response.Write(res.ToJson());
                Response.End();
                return;
            }
            foreach (var cfg in ConfigManage.SystemConfigs())
            {
                Table configtable = new Table
                {
                    Width = new Unit("100%"),
                    Caption = (cfg.GetCustomAttributes(true)[0] as DisplayNameAttribute).DisplayName,
                    CssClass = "TabelGrid"
                };


                var systemconfig = SystemConfigManage.Read(cfg);
                foreach (var property in cfg.GetProperties())
                {
                    #region 属性
                    var display = property.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault() as DisplayNameAttribute;
                    if (display == null)
                    {
                        continue;
                    }
                    TableRow myRow = new TableRow();

                    TableCell titleCell = new TableCell
                    {
                        HorizontalAlign = HorizontalAlign.Right,
                        Text = display.DisplayName + "：",
                        CssClass = "RowTitle"
                    };
                    myRow.Cells.Add(titleCell);

                    TableCell myCell = new TableCell
                    {
                        HorizontalAlign = HorizontalAlign.Left
                    };

                    if (property.PropertyType == typeof(bool))
                    {
                        CheckBox control = new CheckBox()
                        {
                            Checked = Convert.ToBoolean(property.GetValue(systemconfig, null)),

                            ID = cfg.FullName + "_" + property.Name
                        };
                        control.Attributes["value"] = control.Checked.ToString();
                        control.Attributes["onclick"] = "checkChange(this);";
                        myCell.Controls.Add(control);
                    }
                    else
                    {
                        TextBox control = new TextBox
                        {
                            Text = Convert.ToString(property.GetValue(systemconfig, null)),
                            Width = 250,
                            ID = cfg.FullName + "_" + property.Name
                        };
                        myCell.Controls.Add(control);
                    }


                    myRow.Cells.Add(myCell);
                    configtable.Rows.Add(myRow);
                    #endregion
                }
                this.form1.Controls.Add(configtable);

                Button bt = new Button
                {
                    Text = "保存"
                };
                bt.Attributes["typeFullName"] = cfg.FullName;
                bt.Attributes["style"] = "margin-left: auto; margin-right: auto;";
                bt.Width = new Unit("80px");
                bt.OnClientClick = "return submitForm('" + cfg.FullName + "');";
                bt.UseSubmitBehavior = false;
                this.form1.Controls.Add(bt);
            }

        }
 

        private Result Save(string typeFullName)
        {
            Result res = new Result();

            Type cfg = ConfigManage.SystemConfigs(typeFullName);
            var config = SystemConfigManage.Read(cfg);
            foreach (var property in cfg.GetProperties())
            {
                string value = Request.Form[typeFullName + "_" + property.Name];

                object ojb = SystemConfigManage.SetValue(cfg, property.Name, value);
                if (ojb == null)
                {
                    continue;
                }
                property.SetValue(config, ojb, null);
            }

            SystemConfigManage.Save(config);


            return res;
        }
    }
}